//
//  PartnerViewController.m
//  JiFengDaRen
//
//  Created by shouhan on 2018/6/8.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "PartnerViewController.h"
#import "PartnerTableViewCell.h"
#import "PartnerModel.h"
#import "SubModel.h"
#import "RecordViewController.h"
#import "PartnerDetailsController.h"
@interface PartnerViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger pageindex;
}
@property (nonatomic, strong) UITableView * partnerTableView;

@property (nonatomic, strong) NSMutableArray * dataSource;

@end

@implementation PartnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    [self partnerTableView];
    
    pageindex = 1;

    [self DoRequestGet_sub_register:YES];
    
    self.partnerTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    
    self.partnerTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return self.dataSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PartnerTableViewCell * cell = [PartnerTableViewCell tableViewCellWithTableView:tableView];
    if (indexPath.section == 1) {
        cell.model = self.dataSource[indexPath.row];
    }
    [cell getCellIndex:indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return __I6Height(50);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, __I6Height(40))];
    if (section == 1) {
        view.backgroundColor = [UIColor colorWithHexString:@"#F7F8FB"];
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, __I6Height(40))];
        label.centerX = view.centerX;
        label.text = [NSString stringWithFormat:@"总共%ld个客户",(unsigned long)self.dataSource.count];
        label.font = KSHSystemFont(14);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor colorWithHexString:@"#666666"];
        [view addSubview:label];
        return view;
    }else{
        return view;
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 10;
    }else{
        return __I6Height(40);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        RecordViewController * VC = [[RecordViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }else{
        PartnerDetailsController * VC = [[PartnerDetailsController alloc] init];
        SubModel *model = self.dataSource[indexPath.row];
        VC.phone = model.phone;
        [self.navigationController pushViewController:VC animated:YES];
    }
}

// 懒加载
- (UITableView *)partnerTableView{
    if (!_partnerTableView) {
        _partnerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-KNAVIGATIONBARHEIGHT) style:UITableViewStyleGrouped];
        _partnerTableView.delegate = self;
        _partnerTableView.dataSource = self;
        _partnerTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _partnerTableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FB"];
     
        [self.view addSubview:_partnerTableView];
    }
    return _partnerTableView;
}

-(void)refresh
{
    pageindex = 1;
    [self DoRequestGet_sub_register:YES];
}
-(void)loadMore
{
    pageindex ++;
    [self DoRequestGet_sub_register:NO];
}

- (void)DoRequestGet_sub_register:(BOOL)isRemove{
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setValue:[NSString stringWithFormat:@"%ld",(long)pageindex] forKey:@"pageindex"];
    [dict setValue:@"10" forKey:@"count"];
    [dict setValue:[UserModel shareManager].phone forKey:@"cooperator"];

    [PPNetworkHelper POST:Get_sub_register parameters:dict cache:NO encrypt:YES decode:YES responseCache:^(id responseCache) {
        
    } success:^(id result) {
        
        if ([result[@"success"] boolValue] == YES) {
            if (isRemove) {
                [self.dataSource removeAllObjects];
            }
            [self.dataSource addObjectsFromArray:[PartnerModel mj_objectArrayWithKeyValuesArray:result[@"data"]]];
        }else{
            [self.view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        [self.partnerTableView.mj_header endRefreshing];
        [self.partnerTableView.mj_footer endRefreshing];
        [self.partnerTableView reloadData];
        
    } failure:^(NSError *error) {
        [self.partnerTableView.mj_header endRefreshing];
        [self.partnerTableView.mj_footer endRefreshing];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
